package web.mb;



import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import metier.entities.Airport;
import metier.entities.Etat;
import metier.entities.Gare;
import metier.entities.Pays;
import metier.entities.Port;
import metier.entities.Station;
import metier.entities.Ville;
import metier.sess.IEtatLocal;
import metier.sess.IPaysLocal;
import metier.sess.IStationLocal;
import metier.sess.IVilleLocal;


@ManagedBean
@ViewScoped
public class StationMB {
	
	@EJB 
	private IPaysLocal meteirp;
	
	@EJB
	private IEtatLocal metiere;
	
	@EJB 
	private IVilleLocal meteirv;

	@EJB
	private IStationLocal metier;
	
	private Integer Id;
	static private Gare station=new Gare();
	static private Port port=new Port();
	static private Airport airport=new Airport();
	private String type;
	
	//id pays,état,ville
	static private Integer IdS;
	static private Integer IdC;
	static private Integer IdCi;
	
	//liste  pays ,villes,pays
	static private Set<Etat> listeEtat;
	static private Set<Ville> listeVille;
	
	
	
	 
	
	public Station getStation() {
		return station;
	}
	public Port getPort() {
		return port;
	}
	public void setPort(Port port) {
		this.port = port;
	}
	public Airport getAirport() {
		return airport;
	}
	public void setAirport(Airport airport) {
		this.airport = airport;
	}
	
	public void setStation(Gare station) {
		this.station = station;
	}
	public Set<Etat> getListeEtat() {
		return listeEtat;
	}
	public void setListeEtat(Set<Etat> listeEtat) {
		this.listeEtat = listeEtat;
	}
	public Set<Ville> getListeVille() {
		return listeVille;
	}
	public void setListeVille(Set<Ville> listeVille) {
		this.listeVille = listeVille;
	}
	public Integer getIdS() {
		return IdS;
	}
	public void setIdS(Integer idS) {
		IdS = idS;
	}
	public Integer getIdC() {
		return IdC;
	}
	public void setIdC(Integer idC) {
		IdC = idC;
	}
	public Integer getIdCi() {
		return IdCi;
	}
	public void setIdCi(Integer idCi) {
		IdCi = idCi;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	

	
	
	public String Ajouter(Long i)
	{
		
		if(i==0)
		{
		
			station.setVilleStations(meteirv.getVille(IdCi));
			metier.addStation(station);
			
		}
		else if(i==1)
		{
			port.setVilleStations(meteirv.getVille(IdCi));
			metier.addStation(port);
		}
		else
		{
			airport.setVilleStations(meteirv.getVille(IdCi));
			metier.addStation(airport);
		}
		
		
		return "ManageStations.xhtml";
	}
	
	

	
	public String edit(Integer Id,Long i)
	{
		this.Id=Id;
		
		  
		
		if(i==0)
		{
			station=(Gare) metier.getStation(Id);
			IdS=station.getVilleStations().getEtat().getIdEtat();
			IdC=station.getVilleStations().getEtat().getPays().getIdPays();
			IdCi=station.getVilleStations().getIdVille();
			
			listeEtat=meteirp.getPays(IdC).getListEtat();
			listeVille=metiere.getEtat(IdS).getListVille();
			return "EditStation.xhtml";
		}
		else if(i==1)
		{
			port=(Port) metier.getStation(Id);
			IdS=port.getVilleStations().getEtat().getIdEtat();
			IdC=port.getVilleStations().getEtat().getPays().getIdPays();
			IdCi=port.getVilleStations().getIdVille();
			
			listeEtat=meteirp.getPays(IdC).getListEtat();
			listeVille=metiere.getEtat(IdS).getListVille();
			return "EditPort.xhtml";
		}
		else
		{
			airport=(Airport) metier.getStation(Id);
			IdS=airport.getVilleStations().getEtat().getIdEtat();
			IdC=airport.getVilleStations().getEtat().getPays().getIdPays();
			IdCi=airport.getVilleStations().getIdVille();
			
			listeEtat=meteirp.getPays(IdC).getListEtat();
			listeVille=metiere.getEtat(IdS).getListVille();
			return "EditAirPort.xhtml";
		}
		
	
			
		
		
	}
	
	public String Modifier(Long type)
	{
		
		if(type==0)
		{
		station.setVilleStations(meteirv.getVille(IdCi));
		metier.updateStation(station);
		}
		else if(type==1)
		{
			port.setVilleStations(meteirv.getVille(IdCi));
			metier.updateStation(port);
		}
		else
		{
			airport.setVilleStations(meteirv.getVille(IdCi));
			metier.updateStation(airport);
		}
		return "ManageStations.xhtml";
	}

	public String Supprimer(Integer Id)
	{
		
		metier.deleteStation(Id);
		return "succes";
		
	}

	
	
	//onchange pays
	
 public void onCountryChange() {
		 
		
			 if(IdC!=0)
		        {
		        	
				 listeEtat= meteirp.getPays(IdC).getListEtat();
		        }
		        else
		        {
		        	listeEtat=null;
		        }
	    }
 
 
//onchange état
	 
	 public void onStateChange() {
		
		
			 if(IdS!=0)
		        {
		        	
		        	listeVille=metiere.getEtat(IdS).getListVille();
		        }
		        else
		        {
		        	listeVille=null;
		        }
		
		   
	    }
	//liste des pays
	 public List<Pays> getListPays()
		{
			 
			return meteirp.getAllPaysActive();
		}
	 
	 //liste des station a=0 pour station de train,a=1 pour port et a=2 pour les airport
	 public List<Station> listeStation(Integer i)
	 {
		 System.out.println(metier.getAllStation(i).size());
		 return metier.getAllStation(i);
	 }
}
